package com.kly.cashmall.module.order.adapter;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.OrderFieldDetailsBean;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
public class GeneralInformationDisplayAdapter extends BaseAdapter {

    private static final String TAG = "GeneralInformationDisplayAdapter";

    private Activity context;
    private List<OrderFieldDetailsBean> mDetailsBeanList = new ArrayList<>();
    private OnCheckedChangeListener onCheckedChangeListener;
    public GeneralInformationDisplayAdapter(Activity context) {
        this.context = context;
    }
    public GeneralInformationDisplayAdapter(Activity context, OnCheckedChangeListener onCheckedChangeListener) {
        this.context = context;
        this.onCheckedChangeListener = onCheckedChangeListener;
    }


    public void setOrderFieldDetailsBean(List<OrderFieldDetailsBean> detailsBeanList) {
        if (detailsBeanList == null) {
            this.mDetailsBeanList = new ArrayList<>();
        }
        this.mDetailsBeanList.clear();
        this.mDetailsBeanList.addAll(detailsBeanList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDetailsBeanList == null ? 0 : mDetailsBeanList.size();
    }

    @Override
    public int getViewTypeCount() {
        return 10;
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(mDetailsBeanList.get(position).getType());
    }

    @Override
    public Object getItem(int position) {
        return mDetailsBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case 1: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createShowTextView(parent, detailsBean);
            }
            break;
            case 2: {
                convertView = createShowLineView(parent);
            }
            break;
            case 3: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createCheckContractView(parent, detailsBean);
            }
            break;
            case 4: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createShowContractView(parent, detailsBean);
            }
            break;
            case 5: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createShowColorTextView(parent, detailsBean);
            }
            break;
            case 6: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createCopyTextView(parent, detailsBean);
            }
            break;
            case 7: {
                OrderFieldDetailsBean detailsBean = (OrderFieldDetailsBean) getItem(position);
                convertView = createBankUpdateView(parent, detailsBean);
            }
            break;
            default: {
                convertView = new LinearLayout(context);
            }
            break;
        }

        //隐藏下划线
        if (position >= getCount() - 1) {
            View line = convertView.findViewById(R.id.item_line);
            if (line != null) {
                line.setVisibility(View.GONE);
            }
        }

        return convertView;
    }
    /**
     * 文本框 type=1
     */
    private View createShowTextView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_info_text, parent, false);
        ViewHolder holder = ViewHolder.getHolder(view);
        holder.setText(R.id.item_name,detailsBean.getTitle());
        holder.setText(R.id.item_value,detailsBean.getDetail().get(0).getText());
        return view;
    }
    /**
     * 横线 type=2
     */
    private View createShowLineView(ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_info_line, parent, false);
        return view;
    }

    /**
     * 合同单选框 type=3
     */
    private View createCheckContractView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_contract_check, parent, false);
        ViewHolder holder = ViewHolder.getHolder(view);
        CheckBox mLoanAgree = holder.get(R.id.loan_agree);
        mLoanAgree.setOnCheckedChangeListener((compoundButton, b) -> {
            onCheckedChangeListener.checkedChangeListener(compoundButton,b);
        });
        RecyclerView mLinkList = holder.get(R.id.item_link);
        mLinkList.setNestedScrollingEnabled(false);
        mLinkList.setLayoutManager(new GridLayoutManager(context,2));
        LinkAdapter adapter = new LinkAdapter();
        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            BaseActionHelper.with(context).handleAction(adapter.getItem(position).getUrl());
        });
        adapter.setList(detailsBean.getDetail());
        mLinkList.setAdapter(adapter);
        return view;
    }

    /**
     * 合同显示 type=4
     */
    private View createShowContractView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_contract, parent, false);
        ViewHolder holder = ViewHolder.getHolder(view);
        holder.setText(R.id.item_name,detailsBean.getTitle());
        RecyclerView mLinkList = holder.get(R.id.item_link);
        mLinkList.setNestedScrollingEnabled(false);
        if (detailsBean.getDetail().size() == 1){
            mLinkList.setLayoutManager(new GridLayoutManager(context,1));
        }else {
            mLinkList.setLayoutManager(new GridLayoutManager(context,2));
        }
        LinkShowAdapter adapter = new LinkShowAdapter(context);
        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            BaseActionHelper.with(context).handleAction(adapter.getItem(position).getUrl());
        });
        adapter.setList(detailsBean.getDetail());
        mLinkList.setAdapter(adapter);
        return view;
    }

    /**
     * 文本框 type=5
     */
    private View createShowColorTextView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_info_text_color, parent, false);
        ViewHolder holder = ViewHolder.getHolder(view);
        holder.setText(R.id.item_name,detailsBean.getTitle());
        holder.setText(R.id.item_value,detailsBean.getDetail().get(0).getText());
        return view;
    }

    /**
     * 可复制文本  type=6
     */
    private View createCopyTextView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_general_check, parent, false);
        ((TextView) view.findViewById(R.id.item_title)).setText(detailsBean.getTitle());
        ((TextView) view.findViewById(R.id.item_value)).setText(detailsBean.getDetail().get(0).getText());
        view.findViewById(R.id.item_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData mClipData = ClipData.newPlainText("Label", detailsBean.getDetail().get(0).getText());
                cm.setPrimaryClip(mClipData);
                ToastUtil.showToast("copy successful ！");
            }
        });
        return view;
    }

    /**
     * 银行卡修改  type=7
     */
    private View createBankUpdateView(ViewGroup parent, OrderFieldDetailsBean detailsBean) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bank_update, parent, false);
        ((TextView) view.findViewById(R.id.item_title)).setText(detailsBean.getTitle());
        ((TextView) view.findViewById(R.id.item_value)).setText(detailsBean.getDetail().get(0).getText());
        ((TextView) view.findViewById(R.id.item_tips)).setText(detailsBean.getTips());
        view.findViewById(R.id.item_bank_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActionHelper.with(context).handleAction(detailsBean.getDetail().get(0).getUrl());
            }
        });
        return view;
    }


    public interface OnCheckedChangeListener{
        void checkedChangeListener(CompoundButton compoundButton, boolean b);
    }
}
